#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <poll.h>

int main() {
    int fd = open("/dev/mypoll", O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
        perror("open");
        return -1;
    }

    struct pollfd fds;
    fds.fd = fd;
    fds.events = POLLIN;

    printf("Waiting for data...\n");

    int ret = poll(&fds, 1, 10000);  // 10秒超时
    if (ret == 0) {
        printf("Timeout occurred!\n");
    } else if (ret > 0) {
        if (fds.revents & POLLIN) {
            char buf[128];
            read(fd, buf, sizeof(buf));
            printf("Received data: %s\n", buf);
        }
    } else {
        perror("poll");
    }

    close(fd);
    return 0;
}